﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace DSS_Simplex
{
    public partial class Form2 : Form
    {
        string MyConnectionString = "Server=localhost;Database=dss;Uid=root;Pwd=;";
        private string textFilter;
        private DataSet ds;
        Form3 formSearch = null;

        CheckBox chkbox = new CheckBox();

        public Form2()
        {
            InitializeComponent();
            BindDatakeGrid();
        }
        private void BindDatakeGrid() 
        {
            ds = new DataSet();
            MySqlConnection connection = new MySqlConnection(MyConnectionString);
            connection.Open();
            try
            {
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT nama_var, koef_var, min_var, max_var, deskripsi FROM variable";
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds);
                
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
           
                

            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(ex.Message),"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void chkBoxChange(object sender, EventArgs e) 
        {
            for (int k = 0; k <= dataGridView1.RowCount - 1; k++) 
            {
                this.dataGridView1[0, k].Value = this.chkbox.Checked;
            }
            this.dataGridView1.EndEdit();
        }

        private void searchgrid_TextChanged(List<field> fields)
        {
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            textFilter = ""; //reset
            
            bool first = true;
            foreach (field f in fields) 
            {
                if (f.Value.Length > 0) 
                {
                    if (!first) textFilter += " and ";
                    textFilter += f.Field + " like '%" + f.Value + "%'";
                    first = false;
                }
            }
            dv.RowFilter = textFilter;
            this.label1.Text = textFilter;
        }

       private void button1_Click(object sender, EventArgs e)
        {
            formSearch = null;
            textFilter = "";
            this.label1.Text = "Filter: None";
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            dv.RowFilter = textFilter;
        }

       private void button4_Click(object sender, EventArgs e)
       {
           if (formSearch == null) 
           {
               List<field> fields = new List<field>();
               field f = new field();
               f.FriendlyName = "Var Name";
               f.Field = "nama_var";
               fields.Add(f);

               formSearch = new Form3(fields);
               formSearch.searchgrid_TextChanged += new SearchContextChangedHandler(searchgrid_TextChanged); 

           }
           formSearch.ShowDialog();
       }

       private void button3_Click(object sender, EventArgs e)
       {
           int i = 0;
           List<int> CheckedRow = new List<int>();

           for (i = 0; i <= dataGridView1.RowCount - 1; i++) 
           {
               if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true) 
               {
                   CheckedRow.Add(i);
               }
           }
           foreach (int k in CheckedRow) 
           {
               Form1 frm = new Form1();
           }
       }
    }
}